<?php

namespace App\Http\Controllers;

use App\Machine;
use App\Worked;
use App\Workerstask;
use App\Workshop;
use App\Worktask;
use Illuminate\Http\Request;
use Carbon\Carbon;
use App\Work;
use App\Worker;
use App\Rule;
class WorkController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function __construct() {
        $this->middleware('auth');
    }
    public function index(Request $request)
    {
        $uid=\Auth::id();
        $rules=Rule::where('uid',$uid)->get();

     return view('work.home',['rules'=>$rules]);

    }
   
   public function getworks(Request $request)
    {

        if($request->input('date') != null){
            $date=$request->input('date');
        }else{
            $date=Carbon::now();
        }
        $date=date('Y-m-d',strtotime($date) );
        $works=Work::where('date',$date)->get();
        return response()->json($works);
    }

    public function getWorkeds()
    {
        $date=Carbon::now();
        $date=date('Y-m-d',strtotime($date) );
        $workseds=Worked::where('date','>=',$date)->get();
        return response()->json($workseds);
    }
    public function getAll()
    {
        $date=Carbon::now();
        $firstDay=$date->firstOfMonth();
        $date=date('Y-m-d',strtotime($firstDay) );
        $workseds=Worked::where('date','>=',$date)->get();
        return response()->json($workseds);
    }
    public function getAllWorkeds()
    {

        $workseds=Worked::all();
        return response()->json($workseds);
    }
    public function notAllot(Request $request)
    {

        if($request->input('date') != null){
            $date=$request->input('date');
        }else{
            $date=Carbon::now();
        }
        $date=date('Y-m-d',strtotime($date) );
        $works=Work::where('date','>=',$date)->get();
        return response()->json($works);
    }
    public function notAllotInfo(Request $request)
    {
        $date=Carbon::now();
        $date=date('Y-m-d',strtotime($date) );

        $notAllotInfo['allWorkAbles']=Work::where('date','>=',$date)->whereNotNull('workerId')->count();//已排班
        $notAllotInfo['allWorkallots']=Work::where('date','>=',$date)->whereNotNull('workerId')->where('alloted',0)->count();//未排产
        $notAllotInfo['allWorkalloteds']=$notAllotInfo['allWorkAbles']-$notAllotInfo['allWorkallots'];//未排产
        $notAllotInfo['allWorkeds']=Worked::where('date','>=',$date)->count();//已排班天数
        $notAllotInfo['allWorks']=$notAllotInfo['allWorkeds']*2;
        return response()->json($notAllotInfo);
    }
    public function getworkers(Request $request)
    {
        if($request->input('date') != null){
            $date=$request->input('date');
        }else{
            $date=Carbon::now();
        }
        $date=date('Y-m-d',strtotime($date) );
        $workers=Workerstask::where('date',$date)->whereNull('machineId')->get();
        return response()->json($workers);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function addWork(Request $request)
    {
        if($request->input('date') != null){
            $date=$request->input('date');
        }else{
            $date=Carbon::now();
        }

        $date=date('Y-m-d',strtotime($date) );
        $worked=Worked::firstOrCreate(['date'=>$date]);
        $create['workedId']=$worked->id;
        $machines=Machine::all();
        foreach ($machines as $mc){
            $create['date']=$date;
            $create['workType']=0;
            $create['alloted']=0;
            $create['machineId']=$mc->id;
            $create['machineName']=$mc->machineName;
            $create['machineWorkshopId']=$mc->machineWorkshopId;
            $create['machineWorkshopName']=$mc->machineWorkshopName;
            Work::firstOrCreate($create);
            $create['workType']=1;
            Work::firstOrCreate($create);
        }
        $workers=Worker::all();
        foreach($workers as $wk){
            $createworkerstask['date']=$date;
            $createworkerstask['workerId']=$wk->id;
            $createworkerstask['workerName']=$wk->workerName;
            Workerstask::firstOrCreate($createworkerstask);
        }
        $worked=Worked::find($worked->id);
        $works=$worked->works()->get();
        return response()->json($works);
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function setworker(Request $request)
    {
        $date=$request->input('date');
        $date=date('Y-m-d',strtotime($date) );

        $machineId=$request->input('machineId');
        $workType=$request->input('workType');
        $workerId=$request->input('workerId');
        $worker=Worker::find($workerId);

        $workerName=$worker->workerName;



        Workerstask::where('workerId',$workerId)->where('date',$date)->update(['workType'=>$workType,'machineId'=>$machineId]);

        $count=Work::where('machineId',$machineId)->where('date',$date)->where('workType',$workType)->whereNotNull('workerId')->count();
        if ($count > 0){
           $work= Work::where('machineId',$machineId)->where('date',$date)->where('workType',$workType)->whereNotNull('workerId')->get();
            foreach ($work as $wk){
                $changeWorkerId=$wk->workerId;
                        }
            Worktask::where('machineId',$machineId)->where('date',$date)->where('workType',$workType)->update(['workerName'=>$workerName,'workerId'=>$workerId]);
            Work::where('machineId',$machineId)->where('date',$date)->where('workType',$workType)->update(['workerName'=>$workerName,'workerId'=>$workerId]);

            Workerstask::where('workerId',$changeWorkerId)->update(['workType'=>null,'machineId'=>null]);
        }else{
            Worktask::where('machineId',$machineId)->where('date',$date)->where('workType',$workType)->update(['workerName'=>$workerName,'workerId'=>$workerId]);
            Work::where('machineId',$machineId)->where('date',$date)->where('workType',$workType)->update(['workerName'=>$workerName,'workerId'=>$workerId]);
        }

        $workers=Workerstask::where('date',$date)->whereNull('machineId')->get();
        return response()->json($workers);
    }


    public function update(Request $request, $id)
    {
        $input=$request->all();
        return response()->json($input);
    }
    public function workcan(Request $request)
    {
        if($request->input('workId') == 0){
            return response()->json('提交失败');
        }else{
            $work=Work::find($request->input('workId'));
            $work->can=1;
            $work->save();
        }
    }
    public function worknotcan(Request $request)
    {
        if($request->input('workId') == 0){
            return response()->json('提交失败');
        }else{
            $work=Work::find($request->input('workId'));
            $work->can = 0;
            $work->save();
        }
    }
    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
